package com.jokelandave.sportzbetz.client;

import com.google.api.gwt.client.GoogleApiRequestTransport;
import com.google.api.gwt.client.OAuth2Login;
import com.google.api.gwt.services.plus.shared.Plus;
import com.google.api.gwt.services.plus.shared.model.Person;
import com.google.gwt.core.client.Callback;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.jokelandave.sportzbetz.client.events.GoogleLoginEvent;
import com.jokelandave.sportzbetz.client.events.HomeNavigateEvent;
import com.jokelandave.sportzbetz.client.events.IGoogleLoginEventHandler;
import com.jokelandave.sportzbetz.client.events.IHomeNavigateEventHandler;
import com.jokelandave.sportzbetz.client.events.IWelcomeNavigateEventHandler;
import com.jokelandave.sportzbetz.client.events.IProfileNavigateEventHandler;
import com.jokelandave.sportzbetz.client.events.WelcomeNavigateEvent;
import com.jokelandave.sportzbetz.client.events.ProfileNavigateEvent;
import com.jokelandave.sportzbetz.client.presenters.HomePresenter;
import com.jokelandave.sportzbetz.client.presenters.MenuPresenter;
import com.jokelandave.sportzbetz.client.presenters.Presenter;
import com.jokelandave.sportzbetz.client.presenters.ProfilePresenter;
import com.jokelandave.sportzbetz.client.presenters.WelcomePresenter;
import com.jokelandave.sportzbetz.client.views.HomeView;
import com.jokelandave.sportzbetz.client.views.MenuView;
import com.jokelandave.sportzbetz.client.views.ProfileView;
import com.jokelandave.sportzbetz.client.views.WelcomeView;

public class AppController implements ValueChangeHandler<String> {

	final String CLIENT_ID = "832131889587-fes39cgokpl9iismg8eaj6ejtm5999rm.apps.googleusercontent.com";
	final String API_KEY = "AIzaSyCV-mNX5d3vyl6LkQgbKTcdBCglMp7Zr1k";
	final String APPLICATION_NAME = "SportzBetz";
	
	private EventBus eventBus;
	
	private HasWidgets mainContentContainer;
	private HasWidgets menuBarContent;
	
	private Person currentlyLoggedInUser;
	private Plus plus;
	
	public AppController(Plus plus, EventBus eventBus)
	{
		this.eventBus = eventBus;
		this.plus = plus;
		plus.initialize(eventBus, new GoogleApiRequestTransport(APPLICATION_NAME, API_KEY));
		bind();
	}
	
	private void bind()
	{
		History.addValueChangeHandler(this);
		
		eventBus.addHandler(HomeNavigateEvent.TYPE, new IHomeNavigateEventHandler(){

			@Override
			public void onHomeNavigate(HomeNavigateEvent event) 
			{
				History.newItem("home");
			}
			
		});
		
		eventBus.addHandler(ProfileNavigateEvent.TYPE, new IProfileNavigateEventHandler(){

			@Override
			public void onProfileNavigate(ProfileNavigateEvent event) 
			{
				History.newItem("profile");
			}
			
		});
		
		eventBus.addHandler(WelcomeNavigateEvent.TYPE, new IWelcomeNavigateEventHandler(){

			@Override
			public void onWelcomeNavigate(WelcomeNavigateEvent event) 
			{
				menuBarContent.clear();
				WelcomePresenter welcomePresenter = new WelcomePresenter(eventBus, new WelcomeView());
				welcomePresenter.go(mainContentContainer);
			}
			
		});
		
		eventBus.addHandler(GoogleLoginEvent.TYPE, new IGoogleLoginEventHandler(){

			@Override
			public void onGoogleLogin(GoogleLoginEvent event) 
			{
				googleUserLogin();
			}
			
		});
	}
	
	@Override
	public void onValueChange(ValueChangeEvent<String> event) 
	{
		String token = event.getValue();
	    
	    if (token != null) 
	    {
	      Presenter presenter = null;

	      if (token.equals("home")) 
	      {
	        presenter = new HomePresenter(eventBus, new HomeView());
	      }
	      if(token.equals("profile"))
	      {
	    	  presenter = new ProfilePresenter(currentlyLoggedInUser, eventBus, new ProfileView());
	      }
	      if (presenter != null) {
	        presenter.go(mainContentContainer);
	      }
	      
	    }
	} 

	public void go(final HasWidgets mainContentContainer, final HasWidgets menuBarContentContainer) 
	{
		this.mainContentContainer =  mainContentContainer;
		this.menuBarContent = menuBarContentContainer;
		
		
		googleUserLogin();
		
//		loginService.login(GWT.getHostPageBaseURL(), new AsyncCallback<LoginInfo>(){
//
//			@Override
//			public void onFailure(Throwable caught) 
//			{
//				// TODO Auto-generated method stub, display an application error page
//				caught.printStackTrace();
//			}
//
//			@Override
//			public void onSuccess(LoginInfo result) 
//			{
//				if(result.isLoggedIn())
//				{
//					MenuPresenter menuPresenter = new MenuPresenter(result.getLogoutUrl(), result.getNickname(), new MenuView(), eventBus);
//					menuPresenter.go(menuBarContent);
//					History.newItem("home");
//				}
//				else
//				{
//					eventBus.fireEvent(new WelcomeNavigateEvent(result.getLoginUrl()));
//				}
//			}
//			
//		});
	}

	private void googleUserLogin() 
	{
		OAuth2Login.get().authorize(CLIENT_ID, Plus.PlusAuthScope.PLUS_ME, new Callback<Void, Exception>(){

			public void onFailure(Exception reason) 
			{
				reason.printStackTrace();
			}

			@Override
			public void onSuccess(Void result) 
			{
				plus.people().get("me").to(new Receiver<Person>(){

					@Override
					public void onSuccess(Person response) 
					{
						currentlyLoggedInUser = response;
						if(currentlyLoggedInUser != null)
						{
							MenuPresenter menuPresenter = new MenuPresenter(currentlyLoggedInUser, new MenuView(), eventBus);
							menuPresenter.go(menuBarContent);
							History.newItem("home");
						}
						else{
							
						}
					}
					
				}).fire();
			}
			
		});
	}
}
